<?php
/**
* File holding the user settings class
*
* @author       Fabrice Douteaud <clearbudget@douteaud.com>
* @package      framework
* @access       public
*/

/***********************************************************************

  Copyright (C) 2008  Fabrice douteaud (clearbudget@douteaud.com)

    This file is part of ClearBudget.

    ClearBudget is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClearBudget is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClearBudget.  If not, see <http://www.gnu.org/licenses/>.


************************************************************************/

/**
* Class to retreive and set the user preferences. It is used in the context.
* 
* @author       Fabrice Douteaud <clearbudget@douteaud.com>
* @package      framework
* @access       public
*/
class class_settings {
  /**
	* @var object Holds the instance of the current class
	* @access private
	* @static	
	*/
  private static $_instance = null;
  /**
	* @var object Holds an instance of the DB object
	* @access private
	* @static	
	*/
  private static $db = null;
  /**
	* @var string the user language 
	* @access private
	* @static	
	*/
  private static $language = null;
  /**
	* @var string the user username 
	* @access private
	* @static	
	*/
  private static $username = null;
  /**
	* @var string the user password 
	* @access private
	* @static	
	*/
  private static $password = null;
  /**
	* @var boolean flag telling if the site should be secured 
	* @access private
	* @static	
	*/
  private static $secureAccess = null;
  /**
	* @var boolean flag telling if the application is setup correctly 
	* @access private
	* @static	
	*/
  private static $setupDone = false;
  /**
	* @var string the user currency 
	* @access private
	* @static	
	*/
  private static $currency = null;
  /**
	* @var string the report date range
	* @access private
	* @static	
	*/
  private static $range = null;
  /**
	* @var string the report limit month (from the range) 
	* @access private
	* @static	
	*/
  private static $limitMonth = null;
  /**
	* @var boolean flag telling if the user preferences have been loaded already 
	* @access private
	* @static	
	*/
  private static $settingsLoaded = false;
  
  /**
	* Saves the language preference 
	*  	
	* @param string the language
	* @return void
	* @access public
	* @static	
	*/		
  public static function setLanguage($lang) {
    self::$db->addFields('language', $lang);
    // do the update
    self::$db->update('t_settings');
    // set the internal var
    self::$language = $lang;
    }
  
  /**
	* Saves all the user preferences at once 
	*
	*  	
	* @param boolean flag telling if the settings are sufficient to say that the application is fully setup
	* @return void
	* @access public
	* @static	
	*/		
  public static function updateUserSettings($setupDone = false, $isNewUser = false) {
    $doUpdate = false;
    global $context;
    if($context->username!=null) {
      self::$db->addFields('username', $context->username);
      $doUpdate = true;
      }
    if($context->password!=null) {
      self::$db->addFields('password', md5($context->password));
      $doUpdate = true;
      }
    if($context->secureAccess!=null) {
      self::$db->addFields('secureAccess', $context->secureAccess);
      $doUpdate = true;
      }
    if($context->language!=null) {
      self::$db->addFields('language', $context->language);
      $doUpdate = true;
      }
    if($context->currency!=null) {
      self::$db->addFields('currency', $context->currency);
      $doUpdate = true;
      }
    if($context->range!=null) {
      self::$db->addFields('range', $context->range);
      $doUpdate = true;
      }
    if($context->limitMonth!=null) {
      self::$db->addFields('limitMonth', $context->limitMonth);
      $doUpdate = true;
      }
    if($doUpdate) {
      self::$db->addFields('setupDone', $setupDone);
      // do the update
      if($isNewUser) self::$db->insert('t_settings');
      else self::$db->update('t_settings');
      
      // reset the internal flag
      self::$settingsLoaded = false;
      // load the new user settings
      self::getSettings();
      }
    }

  /**
	* Reset the password to null
	*
	* @return void
	* @access public
	* @static	
	*/		
  public static function resetPassword() {
    self::$db->addFields('password', '');
    self::$db->update('t_settings');
    }
    
  /**
	* Insert a new user in the DB 
	*
	*  	
	* @param string the username
	* @param string the password
	* @return void
	* @access public
	* @static	
	*/		
  public static function insertUser($username=null, $password=null) {
    if($username == null || $username = '') {
      return false;
      }
    if($password == null || $password = '') {
      return false;
      }
    self::$db->addFields('username', $username);
    self::$db->addFields('password', md5($password));
    // do the insert
    $userId = self::$db->insert('t_settings');
    return $userId;
    }
    
  /**
	* Gets all the user preferences 
	* 	
	* @return void
	* @access public
	* @static	
	*/		
  public static function getSettings() {
    class_debug::addMsg(__FILE__, 'Getting user settings', DEBUGDEBUG);
    if(self::$settingsLoaded) {
      class_debug::addMsg(__FILE__, 'User settings already loaded', DEBUGDEBUG);
      return;
      }
    
    // set the fields to retreive
    self::$db->addFields('language');
    self::$db->addFields('username');
    self::$db->addFields('password');
    self::$db->addFields('secureAccess');
    self::$db->addFields('setupDone');
    self::$db->addFields('currency');
    self::$db->addFields('range');
    self::$db->addFields('limitMonth');
    // run the query
    self::$db->select('v_settings');
    // since there is only one row and since the view can return only one row, we fetch only one row
    $item = self::$db->fetchRow();
    if($item == false) {
      class_debug::addMsg(__FILE__, 'No user setting yet - application is not setup', DEBUGINFO);
      self::$setupDone = false;
      return false;
      }
    self::$language = (string)$item['language'];
    self::$username = (string)$item['username'];
    self::$password = (string)$item['password'];
    self::$secureAccess = (string)$item['secureAccess'];
    self::$setupDone = (boolean)$item['setupDone'];
    self::$currency = (string)$item['currency'];
    self::$range = (string)$item['range'];
    self::$limitMonth = (string)$item['limitMonth'];
    self::$settingsLoaded = true;
    class_debug::addMsg(__FILE__, 'User settings loaded', DEBUGINFO);
    return true;
    }

  /**
	* Gets the username 
	*  		
	* @return void
	* @access public
	* @static	
	*/		
  public static function getUsername($fromDB = false) {
    if(!$fromDB) return self::$username;
    // set the fields to retreive
    self::$db->addFields('username');
    // run the query
    self::$db->select('v_settings');
    $item = self::$db->fetchRow();
    if($item == false) {
      return false;
      }
    return (string)$item['username'];
    }

  /**
	* Gets the password 
	*  		
	* @return void
	* @access public
	* @static	
	*/		
  public static function getPassword($fromDB = false) {
    if(!$fromDB) return self::$password;
    // set the fields to retreive
    self::$db->addFields('password');
    // run the query
    self::$db->select('v_settings');
    $item = self::$db->fetchRow();
    if($item == false) {
      return false;
      }
    return (string)$item['password'];
    }
  
  /**
	* verify username/password 
	*  		
	* @return void
	* @access public
	* @static	
	*/		
  public static function checkCredentials($username, $password) {
    class_debug::addMsg(__FILE__, 'Checking credential for '.$username.', '.$password, DEBUGDEBUG);
    self::$db->addFields('id');
    // set the fields to retreive
    self::$db->addWhere('username', '=', $username);
    self::$db->addWhere('password', '=', md5($password));
    // run the query
    self::$db->select('v_settings');
    $item = self::$db->fetchRow();
    if($item == false) {
      class_debug::addMsg(__FILE__, 'Credentials are not valid', DEBUGDEBUG);
      return false;
      }
    class_debug::addMsg(__FILE__, 'Credentials are valid', DEBUGDEBUG);
    return true;
    }
  
  /**
	* Tells if the application is setup 
	*  	
	*
	* @return boolean true if setup is OK, false otherwise
	* @access public
	* @static	
	*/		
  public static function setupDone() {
    return self::$setupDone;
    }
  
  /**
	* Returns the currency 
	*  	
	*
	* @return string currency
	* @access public
	* @static	
	*/		
  public static function getCurrency() {
    return self::$currency;
    }
  
  /**
	* Returns the language 
	*  	
	*
	* @return string language
	* @access public
	* @static	
	*/		
  public static function getLanguage() {
    return self::$language;
    }
  
  /**
	* Returns the range 
	*  	
	*
	* @return string range
	* @access public
	* @static	
	*/		
  public static function getRange() {
    // prevent invalid range (null, empty, false)
    if(!is_numeric(self::$range)) self::$range = DEFAULTMONTHRANGE;
    return self::$range;
    }
  
  /**
	* Returns the limitMonth 
	*  	
	*
	* @return string limitMonth
	* @access public
	* @static	
	*/		
  public static function getLimitMonth() {
    return self::$limitMonth;
    }
  
  /**
	* tells if access to the application needs to be secure 
	*  	
	*
	* @return boolean secure access flag
	* @access public
	* @static	
	*/		
  public static function secureAccess() {
    return self::$secureAccess;
    }
  
  /**
	* Provide an instance of the current class 
	*	
	* Implementation of the singleton pattern
	*   		
	* @return object An Instance of this class
	* @access public
	* @static	
	*/  
  public static function getInstance() {
    if(is_null(self::$_instance)) {
      self::$_instance = new self();
      }
    return self::$_instance;
    } // getInstance()

  /**
	* class constructor 
	*	
	* access to this class is private as to implement the singleton pattern
	*   	
	* @return void
	* @access private
	*/
  private function __construct() {
    self::$db = class_db::getInstance();
    self::getSettings();
    } // __construct()
  }
?>